#ifndef SINGLETON_H
#define SINGLETON_H
#include"global.h"
template <typename T>
class Singleton{
protected:
    Singleton() = default;
    Singleton(const Singleton<T>&) = delete;
    Singleton& operator=(const Singleton<T>& st) = delete;
    static std::shared_ptr<T> _instance;
public:
    static std::shared_ptr<T> GetInstance()
    {
        static std::once_flag s_flag;
        std::call_once(s_flag,[&](){
            //为什么这里用new T 而不是采用make_shared呢
            _instance = std::shared_ptr<T>(new T);
        });
        return _instance;
    }

    ~Singleton()
    {
        std::cout<<"this is singleton destruct"<<std::endl;
    }

    void PrintAddress()
    {
        std::cout<<_instance.get()<<std::endl;
    }
};
template<typename T>
std::shared_ptr<T> Singleton<T>::_instance = nullptr;

#endif // SINGLETON_H
